package com.hengpeng.itfints.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.BorrowInfo;
import com.hengpeng.itfinbase.persist.ProjectInfo;
import com.hengpeng.itfints.config.ConfigUtil;
import com.hengpeng.itfints.webcache.PageCacheRefreshUtils;

@Controller
public class IndexController extends BaseController
{
	
	protected Log logger = LogFactory.getLog(this.getClass());
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request,ModelMap map)
	{
		int size = 6;  
		 
		BorrowInfo.BorrowStatus[] status = {BorrowInfo.BorrowStatus.RAISE,BorrowInfo.BorrowStatus.END,
				BorrowInfo.BorrowStatus.LOANING,BorrowInfo.BorrowStatus.REPAYMENTING,
				BorrowInfo.BorrowStatus.REPAYMENTED};
		ServiceResult<List<BorrowInfo>> serviceResult = serviceFactory.getBorrowInfoService().queryList(status,null,1,size,null, null, null, null, null, null);
		map.put("productList", serviceResult.getReturnValue());//投资推荐
		List<Article> zxgg = serviceFactory.getArticleService().findArticleListByGroupId(1l, size);//最新公告
		map.put("zxggList", zxgg);//最新公告 
		List<Article> hkdt = serviceFactory.getArticleService().findArticleListByGroupId(600l, size);//还款动态
		map.put("hkdtList", hkdt);//还款动态
		List<Article> cgal = serviceFactory.getArticleService().findArticleListByGroupId(601l, size);//成功案例
		map.put("cgalList", cgal);//成功案例
		
		ServiceResult<List<ProjectInfo>> projectservice = serviceFactory.getProjectService().queryList(1,size,null,
				null, null,null,ProjectInfo.ProjectType.PROJECTFINACE,null,null,null,null);//找项目信息
		map.put("projectList", projectservice.getReturnValue());
		ServiceResult<List<ProjectInfo>> projectfincservice = serviceFactory.getProjectService().queryList(1,size,null,
				null, null,null,ProjectInfo.ProjectType.PROPERTYTRADE,null,null,null,null);//找资金信息
		map.put("projectFinaceList", projectfincservice.getReturnValue());
		return ConfigUtil.getCommon().getSiteindex();
	}
	/**
	 * 服务中心
	 * @param request
	 * @param map
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/servicecenter")
	public String servicecenter(HttpServletRequest request,ModelMap map, String type)
	{
		int size = 6;  
		List<Article> dataList = new ArrayList<Article>();
		if ("business".equals(type)) {//业务模式
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(500l, size);
		} else if ("invest".equals(type)) {//投资技巧
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(501l, size);
		} else if ("project".equals(type)) {//项目到期公告
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(502l, size);
		} else if ("pay".equals(type)) {//支付公告
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(503l, size);
		} else if ("borrow".equals(type)) {//发标公告
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(504l, size);
		} else if ("notice".equals(type)) {//网站公告
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(1l, size);
		} else if ("job".equals(type)) {//招聘中心
			dataList = serviceFactory.getArticleService().findArticleListByGroupId(700l, size);
		}
		map.put("dataList", dataList);
		map.put("type", type);
		return "/page/servicecenter/index.jsp";
	}
	@RequestMapping(value = "/remove")
	public String removeCache(HttpServletResponse response)
	{
		logger.info("删除首页缓存");
		PageCacheRefreshUtils.reloadIndex();
		try
		{
			response.getWriter().write("success");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return "/index.htm";
	}

	@RequestMapping(value = "/testpost", method = RequestMethod.POST)
	public String testpost(HttpServletRequest request, String name, String value)
	{
		//System.out.println(name);
		//System.out.println(value);
		return "/test/test.jsp";
	}

	@RequestMapping(value = "/testget", method = RequestMethod.GET)
	public String testget(HttpServletRequest request, String name, String value)
	{
		//System.out.println(name);
		//System.out.println(value);
		return "/test/test.jsp";
	}

	@RequestMapping(value = "/aaa/{a}")
	public String aaa(@PathVariable Integer a)
	{
		//System.out.println(a);
		return "/test/test.jsp";
	}

	@RequestMapping(value = "/bbb/{a}-{b}")
	public String bbb(@PathVariable Integer a, @PathVariable Integer b)
	{
		//System.out.println(a);
		//System.out.println(b);
		return "/test/test.jsp";
	}
	/**
	 * 转向公告结果页面
	 * @param request
	 * @param map
	 * @param backurl
	 * @param message
	 * @return
	 */
	@RequestMapping(value = "/to_result", method = RequestMethod.POST)
	public String to_result(HttpServletRequest request,ModelMap map, String backurl, String message) {
		map.put("backurl", backurl);
		map.put("message", message);
		return "/page/common/result_back.jsp";
	}
}
